package com.yonghui.summer.ioc.factory;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.View;
import androidx.core.content.ContextCompat;

import java.util.*;

public class SingletonRegistry {

    private static SingletonRegistry instance;
    private static Map factoryMap = new HashMap();

    //私有化构造方法
    private SingletonRegistry() {

    }

    public synchronized static SingletonRegistry getInstance() {
        if (instance == null) {
            instance = new SingletonRegistry();
        }
        return instance;
    }

    public List<Object> getBeans(){
        List  items = new ArrayList();
        Set<Map.Entry<String,Object>> entries = factoryMap.entrySet();
        for(Map.Entry<String,Object> entry : entries){
            if(entry.getValue() != null) {
                items.add(entry.getValue());
            }
        }
        return items;
    }

    public synchronized Object getBeansOfType(Class f) {
        Object o = factoryMap.get(f.getName());
        if (o != null) {
            return o;
        }
        for (Object key : factoryMap.keySet()) {
            Object value = factoryMap.get(key);
            if (value == null) {
                continue;
            }
            if (f.isAssignableFrom(value.getClass())) {
                return value;
            }
        }
        return o;
    }

    public synchronized Object getBeanFactory(String factoryName, Class f) {
        Object bean = null;
        try {
            bean = f.newInstance();
            //将Bean工厂注册到注册表
            factoryMap.put(factoryName, bean);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return bean;
    }

    public synchronized Object getBeanFactory(String factoryName, Object bean) {
        try {
            //将Bean工厂注册到注册表
            factoryMap.put(factoryName, bean);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return bean;
    }

    public synchronized Object getBean(String factoryName) {
        //在注册表中查找看是否有这个BeanFactory的实例
        Object o = factoryMap.get(factoryName);
        return o;

    }

    public synchronized Object getBeanFactory(String factoryName) {
        Object f = null;
        //在注册表中查找看是否有这个BeanFactory的实例
        Object o = factoryMap.get(factoryName);

        if (o != null) {
            return o;
        }

        try {
            f = Class.forName(factoryName).newInstance();

            //将Bean工厂注册到注册表
            factoryMap.put(factoryName, f);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return f;

    }

    public synchronized Object getBeanFactory(Activity activity, Integer rId, Class tClass) {
        String resId = "R_" + rId.toString();

        //在注册表中查找看是否有这个BeanFactory的实例
        Object o = factoryMap.get(resId);

        if (o != null) {
            return o;
        }
        Object resource = null;
        if (String.class.isAssignableFrom(tClass)) {
            resource = activity.getString(rId);
        }
        if (Drawable.class.isAssignableFrom(tClass)) {
            resource = ContextCompat.getDrawable(activity.getApplicationContext(), rId);
        }
        if (Integer.class.isAssignableFrom(tClass) || int.class == tClass) {
            resource = ContextCompat.getColor(activity, rId);
        }
        if (View.class.isAssignableFrom(tClass)) {
            resource = activity.findViewById(rId);
        }
        if (CharSequence.class.isAssignableFrom(tClass) && resource == null) {
            resource = activity.getText(rId);
        }

        try {
            //将Bean工厂注册到注册表
            factoryMap.put(resId, tClass);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return resource;

    }
}

